<template>
  <el-container class="area-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">省市区列表</span>
          <div class="tip"
            >数据来源：<a
              href="http://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2022/"
              target="_blank"
              >2022年国家统计局区划代码</a
            >
            更新时间2022-10-31</div
          >
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button v-auth="'shop.admin.data.area.add'" icon="Plus" type="primary" @click="addRow"
            >新建</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <div class="list-head sa-flex sa-row-between">
        <div>名称</div>
        <div class="sa-flex">
          <div class="list-head-level">级别</div>
          <div>操作</div>
        </div>
      </div>
      <el-tree v-if="tree.data.length > 0" :data="tree.data" node-key="id" ref="accessTree">
        <template #empty>
          <sa-empty />
        </template>
        <template #default="{ node, data }">
          <div class="sa-flex sa-row-between tree-box">
            <div class="sa-flex tree-name">
              <div class="adcode"> #{{ data.id }} </div>
              <div class="tree-name-title sa-line-1">
                {{ data.name }}
              </div>
            </div>
            <div class="sa-flex sa-m-r-10">
              <div class="level">
                {{ data.level == 'province' ? '省级' : data.level == 'city' ? '市级' : '区级' }}
              </div>
              <el-button
                v-auth="'shop.admin.data.area.detail'"
                class="is-link"
                type="primary"
                @click="editRow(node)"
                >编辑</el-button
              >
              <el-popconfirm
                width="fit-content"
                confirm-button-text="确认"
                cancel-button-text="取消"
                title="确认删除这条记录?"
                @confirm="remove(node.data.id)"
              >
                <template #reference>
                  <el-button v-auth="'shop.admin.data.area.delete'" class="is-link" type="danger"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </div>
          </div>
        </template>
      </el-tree>
    </el-main>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../data.service';
  import { useModal } from '@/sheep/hooks';
  import AreaEdit from './edit.vue';
  // 列表
  const tree = reactive({
    data: [],
    order: 'asc',
    sort: '',
    selected: [],
  });
  const loading = ref(true);
  // 获取
  async function getData() {
    loading.value = true;
    const { error, data } = await api.area.list({
      order: tree.order,
      sort: tree.sort,
    });
    if (error === 0) {
      tree.data = data;
    }
    loading.value = false;
  }

  // table 字段排序
  function fieldFilter({ prop, order }) {
    table.order = order == 'ascending' ? 'asc' : 'desc';
    table.sort = prop;
    getData();
  }
  function addRow() {
    useModal(
      AreaEdit,
      { title: '新建', type: 'add' },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  function editRow(row) {
    useModal(
      AreaEdit,
      {
        title: '编辑',
        type: 'edit',
        id: row.data.id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  // 删除api 单独批量可以直接调用
  async function remove(id) {
    await api.area.delete(id);
    getData();
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .area-view {
    .tip {
      line-height: 20px;
      font-size: 12px;
      font-weight: 400;
      color: var(--sa-subfont);
      margin-left: 8px;
      a {
        color: var(--sa-subfont);
        text-decoration: underline;
      }
    }
    .el-main {
      background: var(--sa-background-assist);
      .list-head {
        width: 100%;
        height: 40px;
        padding: 0 40px 0 24px;
        font-size: 14px;
        background: var(--sa-table-header-bg);
        color: var(--sa-subtitle);
        .list-head-level {
          margin-right: 54px;
        }
      }
      .tree-box{
        width: 100%;
      }
      .tree-name {
        font-size: 14px;
        color: var(--sa-font);
        .adcode {
          margin-right: 10px;
          color: #999;
        }
        @media only screen and (max-width: 768px) {
          .tree-name-title {
            width: 90px;
          }
        }
      }
      .level {
        font-size: 14px;
        color: var(--sa-font);
        margin-right: 30px;
      }
    }
  }
  :deep() {
    .el-tree-node__content {
      height: 48px;
    }
    .el-tree-node__label {
      width: 100%;
    }
    .el-tree-node__expand-icon {
      margin-left: 10px;
    }
  }
</style>
